package com.example.qqemailtest.controller;

import com.example.qqemailtest.bean.User;
import com.example.qqemailtest.service.GetCodeService;
import com.example.qqemailtest.service.RegistryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
public class LoginController {

    @RequestMapping({"/login.html", "/"})
    public String login() {
        return "login";
    }

//    -----------------------------------------

    @Autowired
    GetCodeService getCodeService;
    @Autowired
    RegistryService registryService;

    /**
     * 这里至少需要邮箱,其他信息可以不要
     * 用户获取注册码
     *
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("/user/getCode")
    public Map<String, Boolean> getCode(User user) {
//        System.out.println(user);
        log.info(user.toString());
        getCodeService.getCode(user);
        Map<String, Boolean> map = new HashMap<>();
        map.put("success", true);
        return map;
    }

    /**
     * 用户进行注册
     *
     * @param user
     * @return
     */
    @PostMapping("/user/registry")
    public String registry(User user) {
//        返回0代表未注册,返回1 代表注册成功
        Integer registry = registryService.registry(user);
        if (registry == 0) {
//            验证错误,
            return "index";

        } else {
            return "login";
        }

    }


    @RequestMapping("/user/registry2")
    public String registry2() {
        return "redirect:/user/re";

    }
    @RequestMapping("/user/re")
    public String registry(){
        return "index";
    }
//    登入
    @PostMapping("/user/login")
    public String login(User user) {
        String login = registryService.login(user);
        if (login.equals("登录成功")) {
            return "main";
        }
        return "login";
    }


}
